package com.ailanyin.admin.modules.system.controller;

import com.ailanyin.admin.modules.system.service.SysFileService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2022/2/28 0028 上午 9:40
 */
@RestController
@RequestMapping("/common")
@Api(tags = "文件上传下载")
public class SysFileController {

    @Autowired
    private SysFileService sysFileService;

    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public AjaxResult fileUpload(MultipartFile file) {
        String url = sysFileService.fileUpload(file);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", url);
        ajax.put("fileName", url.substring(url.lastIndexOf("/")));
        return ajax;
    }

    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public void fileDownload(String fileName, HttpServletResponse response) throws Exception {
        sysFileService.fileDownload(fileName, response);
    }
}
